package com.cm.kits;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;

/**
 * 日期时间工具
 *
 * @author LWang 2023.02.01
 * @since 1.0.0
 */
public final class DateTimeKits {
    private DateTimeKits() {
    }

    /**
     * 计算今日剩余时间
     *
     * @param unit 时间单位，只支持 SECONDS，MINUTES，HOURS
     * @return 剩余时间
     */
    public static long timeRemaining(ChronoUnit unit) {
        var now = LocalDateTime.now();
        var tomorrow = now.plusDays(1).withHour(0).withMinute(0).withSecond(0);
        var duration = Duration.between(now, tomorrow);
        switch (unit) {
            case SECONDS -> {
                return duration.toSeconds();
            }
            case MINUTES -> {
                return duration.toMinutes();
            }
            case HOURS -> {
                return duration.toHours();
            }
            default -> throw new IllegalArgumentException("时间单位不符合预期！");

        }
    }

    /**
     * 获取当前日期（yyyy-MM-dd）格式
     *
     * @return 当前日期
     */
    public static String currentDay() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.CHINA);
        return formatter.format(LocalDate.now());
    }

    public static void main(String[] args) {
        System.out.println(currentDay());
    }
}
